package at.ac.univie.zsu.aguataplan.domain;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Version;

/**
 * @author gerry
 * 
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class EventAttributeValue implements Comparable<EventAttributeValue> {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column
	protected Long id;

	@Version
	@Column
	protected Integer version;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	protected Set<EventAttribute> refEventAttribute = new HashSet<EventAttribute>();

	@ManyToOne(cascade = CascadeType.ALL)
	protected DetectedEvent refDetectedEvent;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public EventAttributeValue() {

	}

	// ----------------------------------------------------
	// METHODS ABSTRACT
	// ----------------------------------------------------

	public String toStringRelation() {
		StringBuffer sb = new StringBuffer();
		for (EventAttribute ea : refEventAttribute) {
			sb.append(id + " -> " + ea.toString());
			sb.append("\n");
		}

		return sb.toString();
	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public boolean addEventAttribute(EventAttribute eventAttribute) {
		return this.refEventAttribute.add(eventAttribute);
	}

	public boolean deleteEventAttribute(EventAttribute eventAttribute) {
		Iterator<EventAttribute> it = refEventAttribute.iterator();
		long i;
		i = eventAttribute.getId();
		long j = -1;

		while (it.hasNext()) {
			EventAttribute e = it.next();
			j = e.getId();
			if (i == j) {
				it.remove();
				return true;
			}
		}
		return false;
	}

	public void cleanEventAttribute() {
		this.refEventAttribute = new HashSet<EventAttribute>();
	}

	public String toStringExtended() {
		StringBuilder sb = new StringBuilder();
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("RefEventAttribute: ")
				.append(getRefEventAttribute() == null ? "null" : getRefEventAttribute().size()).append(", ");
		sb.append("RefDetectedEvent: ").append(getRefDetectedEvent()).append(", ");
		sb.append("Version: ").append(getVersion());
		return sb.toString();
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Set<EventAttribute> getRefEventAttribute() {
		return refEventAttribute;
	}

	public void setRefEventAttribute(Set<EventAttribute> refEventAttribute) {
		this.refEventAttribute = refEventAttribute;
	}

	public DetectedEvent getRefDetectedEvent() {
		return refDetectedEvent;
	}

	public void setRefDetectedEvent(DetectedEvent refDetectedEvent) {
		this.refDetectedEvent = refDetectedEvent;
	}

}
